<?php
/**
 * @author Micou13630
 * Fichier index.php
 */

// Import Glyphe
require_once 'glypheInit.php';
require_once 'glypheAuth.php';
require_once 'glypheLog.php';
require_once 'glypheTrans.php';

// SESSION Start
session_start();

// Console
include_once Config::getParams()->PATH_GLYPHE.'core/Console.php';

// Import auto
function __autoload($class_name) {
	$inc_path = "";
	if (substr($class_name, strlen($class_name) - 4) === 'Item')
		$inc_path = Config::getParams()->PATH_GLYPHE.'core/formItem/'.$class_name.'.php';
	elseif (substr($class_name, strlen($class_name) - 4) === 'Type')
		$inc_path = Config::getParams()->PATH_GLYPHE.'core/type/'.$class_name.'.php';
	elseif (substr($class_name, strlen($class_name) - 3) === 'Log')
		$inc_path = Config::getParams()->PATH_GLYPHE.'core/log/'.$class_name.'.php';
	elseif (substr($class_name, strlen($class_name) - 9) === 'Validator')
		$inc_path = Config::getParams()->PATH_GLYPHE.'core/type/validator/'.$class_name.'.php';
	elseif (substr($class_name, strlen($class_name) - 10) === 'DataSource')
		$inc_path = Config::getParams()->PATH_GLYPHE.'core/ds/'.$class_name.'.php';
	elseif (substr($class_name, strlen($class_name) - 9) === 'Connector')
		$inc_path = Config::getParams()->PATH_GLYPHE.'core/connector/'.$class_name.'.php';
	elseif (substr($class_name, strlen($class_name) - 3) === 'Dao')
		$inc_path = Config::getParams()->PATH_GLYPHE.'core/dao/'.$class_name.'.php';
	elseif (substr($class_name, 0, 4) === 'Http')
		$inc_path = Config::getParams()->PATH_GLYPHE.'core/http/'.$class_name.'.php';
	elseif ($class_name == Config::getParams()->AUTHENTICATION_CLASSNAME)
		$inc_path = Config::getParams()->AUTHENTICATION_PATH.$class_name.'.php';
	elseif ($class_name == Config::getParams()->TRANSLATOR_CLASSNAME)
		$inc_path = Config::getParams()->TRANSLATOR_PATH.$class_name.'.php';
	else
		$inc_path = Config::getParams()->PATH_GLYPHE.'core/'.$class_name.'.php';
		
	if (is_file($inc_path)) {
		ob_start();
		include_once $inc_path;
		$contents = ob_get_contents();
		ob_end_clean();
		echo $contents;
	} else {
		MessageCollectionLog::getInstance()->addMessage(new MessageLog(MessageLog::TYPE_WARN, $inc_path, 'ClassName inconnu'));
	}
}

/*-------------------------------------------------
// Surcharge gestionnaire erreur
set_error_handler( function($type, $msg, $file, $line, $context = array() ) {
	MessageCollectionLog::getInstance()->addMessage(new MessageLog(MessageLog::TYPE_ERROR, addcslashes(($file."{".$line."}"), '\\'), addcslashes($msg, '\\')));
});

// Sortie off 
//error_reporting(0);

// Pour erreur fatale
register_shutdown_function(function() {
	$error = error_get_last();
	MessageCollectionLog::getInstance()->addMessage(new MessageLog(MessageLog::TYPE_ERROR, addcslashes(($error['file']." [".$error['line']."]"), '\\'), addcslashes($error['message'], '\\')));
	
	if($error !== null) {
        $httpResponse = new HTTPResponse();
        $httpResponse->getError();
    }
});
----------------------------------------------------*/

// Authentication 
if (isset(Config::getParams()->AUTHENTICATION) && Config::getParams()->AUTHENTICATION == 1) {
	if (!isset($_SESSION["#AUTH#"])) {
		$c = Config::getParams()->AUTHENTICATION_CLASSNAME;
		$_SESSION["#AUTH#"] = new $c;
	}
} else {
	$_SESSION["#AUTH#"] = null;
} 

// Translator 
if (isset(Config::getParams()->TRANSLATOR) && Config::getParams()->TRANSLATOR == 1) {
	if (!isset($_SESSION["#TRANSLATOR#"])) {
		$c = Config::getParams()->TRANSLATOR_CLASSNAME;
		$_SESSION["#TRANSLATOR#"] = new $c;
	}
} else {
	$_SESSION["#TRANSLATOR#"] = null;
} 

// Gestion HTTP
$httpRequest 	= new HttpRequest($_GET, $_POST);
$httpResponse 	= new HttpResponse($httpRequest);
$httpResponse->getContent();
?>